package com.budingge.common.ftputils;

import java.util.Properties;

/**
 * 
* @ClassName: FtpClientCfg
* @Description: FTPClient 工具配置类
* @author budingge.com
* @date 2014年5月1日 下午11:44:48
*
 */
public class FtpClientCfg {
	String server;
	int port;
	String user;
	String password;
	String path;
	int conn_time_out = 0;
	int data_time_out = 0;

	boolean local_passive_mode = true;
	String encoding = "gbk";

	public FtpClientCfg(String server, int port, String user, String password,
			String path, int conn_time_out, int data_time_out,
			boolean local_passive_mode, String encoding) {
		super();
		this.server = server;
		this.port = port;
		this.user = user;
		this.password = password;
		this.path = path;
		this.conn_time_out = conn_time_out;
		this.data_time_out = data_time_out;
		this.local_passive_mode = local_passive_mode;
		this.encoding = encoding;
	}

	/**
	 * @return the local_passive_mode
	 */
	public boolean isLocal_passive_mode() {
		return local_passive_mode;
	}

	/**
	 * @param local_passive_mode
	 *            the local_passive_mode to set
	 */
	public void setLocal_passive_mode(boolean local_passive_mode) {
		this.local_passive_mode = local_passive_mode;
	}

	/**
	 * @return the conn_time_out
	 */
	public int getConn_time_out() {
		return conn_time_out;
	}

	/**
	 * @param conn_time_out
	 *            the conn_time_out to set
	 */
	public void setConn_timOe_out(int conn_time_out) {
		this.conn_time_out = conn_time_out;
	}

	/**
	 * @return the data_time_out
	 */
	public int getData_time_out() {
		return data_time_out;
	}

	/**
	 * @param data_time_out
	 *            the data_time_out to set
	 */
	public void setdata_time_out(int data_time_out) {
		this.data_time_out = data_time_out;
	}

	/**
	 * @return the server
	 */
	public String getServer() {
		return server;
	}

	/**
	 * @param server
	 *            the server to set
	 */
	public void setServer(String server) {
		this.server = server;
	}

	/**
	 * @return the port
	 */
	public int getPort() {
		return port;
	}

	/**
	 * @param port
	 *            the port to set
	 */
	public void setPort(int port) {
		this.port = port;
	}

	/**
	 * @return the user
	 */
	public String getUser() {
		return user;
	}

	/**
	 * @param user
	 *            the user to set
	 */
	public void setUser(String user) {
		this.user = user;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the path
	 */
	public String getPath() {
		return path;
	}

	/**
	 * @param path
	 *            the path to set
	 */
	public void setPath(String path) {
		this.path = path;
	}

	public static class FtpClientCfgUtils {

		private static String HOST = "ftp.server";
		private static String PORT = "ftp.port";
		private static String USER = "ftp.user";
		private static String PWD = "ftp.password";
		private static String ENCODING = "ftp.encoding";
		private static String BASE_REMOTE = "ftp.base_remote";
		private static String CONNECTION_TIME_OUT = "ftp.connection_time_out";
		private static String DATA_READ_TIME_OUT = "ftp.data_read_time_out";

		private static String LOCAL_PASSIVE_MODE = "ftp.local_passive_mode";
		private final static String CONNECTION_TIME_OUT_DEFAULT = "60000";
		private final static String DATA_READ_TIME_OUT__DEFAULT = "60000";

		public static FtpClientCfg createFtpClientCfg(Properties prop)
				throws Exception {
			String server = prop.getProperty(HOST);
			int port = Integer.parseInt(prop.getProperty(PORT).toString());
			String user = prop.getProperty(USER, "anonymous");
			if (user == null) {
				throw new Exception("user must be no null");
			}
			String password = prop.getProperty(PWD, "");

			if (password == null) {
				throw new Exception("pwd must be no null");
			}

			String encoding = prop.getProperty(ENCODING, "utf8");
			if (encoding == null) {
				throw new Exception("charset must be no null");
			}
			String base_remote = prop.getProperty(BASE_REMOTE, "/");
			if (base_remote == null) {
				throw new Exception("base_remote must be no null");
			}

			if (!base_remote.startsWith("/") || !base_remote.endsWith("/")) {
				throw new Exception(
						"base_remote must be start with '/' and end with '/'");
			}
			int conn_time_out = Integer.parseInt(prop.getProperty(
					CONNECTION_TIME_OUT, CONNECTION_TIME_OUT_DEFAULT));
			int data_time_out = Integer.parseInt(prop.getProperty(
					DATA_READ_TIME_OUT, DATA_READ_TIME_OUT__DEFAULT));
			boolean local_passive_mode = Boolean.parseBoolean(prop.getProperty(
					LOCAL_PASSIVE_MODE, "true"));
			FtpClientCfg ftpClientCfg = new FtpClientCfg(server, port, user,
					password, base_remote, conn_time_out, data_time_out,
					local_passive_mode, encoding);
			return ftpClientCfg;
		}

	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + conn_time_out;
		result = prime * result + data_time_out;
		result = prime * result
				+ ((encoding == null) ? 0 : encoding.hashCode());
		result = prime * result + (local_passive_mode ? 1231 : 1237);
		result = prime * result
				+ ((password == null) ? 0 : password.hashCode());
		result = prime * result + ((path == null) ? 0 : path.hashCode());
		result = prime * result + port;
		result = prime * result + ((server == null) ? 0 : server.hashCode());
		result = prime * result + ((user == null) ? 0 : user.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		FtpClientCfg other = (FtpClientCfg) obj;
		if (conn_time_out != other.conn_time_out)
			return false;
		if (data_time_out != other.data_time_out)
			return false;
		if (encoding == null) {
			if (other.encoding != null)
				return false;
		} else if (!encoding.equals(other.encoding))
			return false;
		if (local_passive_mode != other.local_passive_mode)
			return false;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		if (path == null) {
			if (other.path != null)
				return false;
		} else if (!path.equals(other.path))
			return false;
		if (port != other.port)
			return false;
		if (server == null) {
			if (other.server != null)
				return false;
		} else if (!server.equals(other.server))
			return false;
		if (user == null) {
			if (other.user != null)
				return false;
		} else if (!user.equals(other.user))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "FtpClientCfg [server=" + server + ", port=" + port + ", user="
				+ user + ", password=" + password + ", path=" + path
				+ ", conn_time_out=" + conn_time_out + ", data_time_out="
				+ data_time_out + ", local_passive_mode=" + local_passive_mode
				+ ", encoding=" + encoding + "]";
	}

}
